package com.sp.scmdb.action;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.ParameterBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.common.action.*;
import com.sp.scmdb.entity.Parameter;
import com.sp.scmdb.service.ParameterService;


@Controller("parameterAction")
@Scope("prototype")
public class ParameterAction extends BaseAction<ParameterBean>{

	private static final long serialVersionUID = 1L;
	private ParameterService parameterService;
	private static Log log = LogFactory.getLog(ParameterAction.class);

	/**
	 * @param parameterService the parameterService to set
	 */
	@Autowired
	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}
	
	
	@SuppressWarnings("unchecked")
	public String findAll() {
		List<Parameter> ParList = parameterService.queryParameter();
		request.setAttribute("ParList", ParList);
		session.put("useType", CommonParamter.TypeCommon.getUseType());
		/*2011-05-04修改**/
		// 页面”添加“，”修改“，”删除“按钮的权限设置
		// 调用BaseAction的button(String urlAdd, String urlEdit, String urlDel)方法
		this.button("Super.Popup(5,'添加参数','/SCMDB/parameterAction!newParameter.action','',400,280)", 
				"Super.editData('/SCMDB/parameterAction!findParameter.action?id=',400,280)", 
				"Super.delData('/SCMDB/parameterAction!delParameter.action?id=')");
		return SUCCESS;
	}
	public String newParameter(){
		return "newSucc";
	}
	public String addParameter() {
		try {
			UserBean oldUser = (UserBean)session.get("user");
			oldUser.setOldUserName(oldUser.getUserName());
			String flg=parameterService.addParameter(entity,oldUser);
			messageSession(flg,"参数");
			if (log.isInfoEnabled()) {
				log.info("add suncce");
			}
		} catch (Exception e) {
			log.error("add fail,reason:"+e.getMessage());
		}
		return "commitSucc";
	}

	public String findParameter() {
		try {
			String pId = request.getParameter("id");
			String Hql = "from Parameter where id = "+pId;
			List<Parameter> list = parameterService.queryParameterName(Hql);
			if (CollectionUtils.isEmpty(list)) {
				request.setAttribute("message", "参数不存在！");
			} else {
				request.setAttribute("ParList", list);
			}
		} catch (Exception e) {
			log.error("add fail"+e.getMessage());
		}
		return "editSucc";
	}
	

	public String editParameter() {
		try {
			UserBean oldUser = (UserBean)session.get("user");
			oldUser.setOldUserName(oldUser.getUserName());
			String flg=parameterService.editParameter(entity,oldUser);
			messageSession(flg,"参数");
			if (log.isInfoEnabled()) {
				log.info("edit suncce");
			}
		} catch (Exception e) {
			log.error("edit fail"+e.getMessage());
		}
		return "commitSucc";
	}

	public String delParameter() {
		String flg=null;
		try {
			String pId = request.getParameter("id");
			UserBean oldUser = (UserBean)session.get("user");
			oldUser.setOldUserName(oldUser.getUserName());
			if(StringUtils.isNotEmpty(pId)){
				String []arr = pId.split(",");
				 flg=parameterService.delParameter(arr,oldUser);
			}
			if ("2".equals(flg)) {
				messageSession("0", "已使用的参数不能删除！");
			}
			if ("1".equals(flg)) {
				messageSession(flg, "参数");
			}
			if (log.isInfoEnabled()) {
				log.info("delete suncce");
			}
		} catch (Exception e) {
			log.error("delete fail"+e.getMessage());
		}
		return "commitSucc";
	}


}
